package com.db.protocol.tags;

import com.db.protocol.exception.IncorrectValueProtocolException;
import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;

/**
 * @author nulls.narik
 */
public enum MsgType {

    LOGON('A'),
    LOGOUT('5'),
    NEW_TRADE('D'),
    REQUEST_CANCEL_TRADE('F'),
    REPORT('8'),
    REJECT('3');

    private final char msgType;

    public char value() {
        return msgType;
    }

    MsgType(char msgType) {
        this.msgType = msgType;
    }

    public static Tag tag() {
        return Tag.MSG_TYPE;
    }

    public static MsgType parse(String message) throws ParseProtocolException {
        if (message == null) {
            throw new NotParsingValueProtocolException(tag());
        }
        try {
            char value = message.charAt(0);

            for (MsgType type : values()) {
                if (type.value() == value) {
                    return type;
                }
            }
        } catch (IndexOutOfBoundsException exception) {
            throw new NotParsingValueProtocolException(tag());
        }
        throw new IncorrectValueProtocolException(tag());
    }

}
